#include "head.h"

int recv_file(int net_fd, char *file_all_name)
{   
    int file_fd =open(file_all_name,O_RDWR|O_CREAT,0666);

    // 接收文件大小
    int file_size;
    recv(net_fd, &file_size, sizeof(int), MSG_WAITALL);

    // 接收文件内容
    int cur_size=0;
    while(cur_size<file_size)
    {
        char buf[1000]={0};
        int ret;
        if(file_size-cur_size < 1000){
            ret =recv(net_fd,buf,file_size - cur_size,MSG_WAITALL);
        }else{
            ret =recv(net_fd,buf,sizeof(buf),MSG_WAITALL);
        }
        if(ret<0)
        {
            close(file_fd);
            return -1;
        }
        write(file_fd,buf,ret);
        cur_size += ret;
    }

    close(file_fd);
    return 0;
}

// 有同名文件的处理逻辑
int same_file(int net_fd,char * file_all_path){


    // 接收文件大小
    int file_size;
    recv(net_fd, &file_size, sizeof(int), MSG_WAITALL);

    // 
    int file_fd =open(file_all_path,O_RDWR|O_APPEND);
    struct stat stat_file;
    fstat(file_fd,&stat_file);
    int server_file_size=stat_file.st_size;

    // 比较, 确定是断点续传
    if(server_file_size < file_size){
        // 确定是断点续传
        self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
        send_mes->cmd=210; 
        // 带上文件大小的信息
        send_mes->para_num=1;
        strcat(send_mes->message,"server_file_size=");
        char str[10];
        snprintf(str, sizeof(str), "%d", server_file_size);
        strcat(send_mes->message, str);
        send_mes->message_len=strlen(send_mes->message);

        send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

        // 接收数据 
        int cur_size=server_file_size;
        while(cur_size<file_size)
        {
            char buf[1000]={0};
            int ret;
            if(file_size-cur_size < 1000){
                ret =recv(net_fd,buf,file_size - cur_size,MSG_WAITALL);
            }else{
                ret =recv(net_fd,buf,sizeof(buf),MSG_WAITALL);
            }
            if(ret<0)
            {
                close(file_fd);
                free(send_mes);
                return -1;
            }
            write(file_fd,buf,ret);
            cur_size += ret;
        }

        close(file_fd);
        // 文件接收完毕
        return 0;

    }else{
        self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
        send_mes->cmd=220; 
        send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
        close(file_fd);
    }

    return 0;
}

int do_download(client_status_t *cs)
{
    int net_fd = cs->socket_fd ;

    //获取文件名
    char command[60]={0};

    strcpy(command,cs->runtime_cmd);

    char * file_name=strtok(command," ");

    file_name=strtok(NULL," ");
    self_defined_pro_t *mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    mes->cmd=download;
    mes->para_num=3;

    strcat(mes->message,"username=");
    strcat(mes->message,cs->user );
    strcat(mes->message,"&");
    strcat(mes->message,"path=");
    strcat(mes->message,cs->path);
    strcat(mes->message,"&");
    strcat(mes->message,"filename=");
    strcat(mes->message,file_name);
    mes->message_len=strlen(mes->message);
    //first send struct
    int ret = send(net_fd,mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
    ERROR_CHECK(ret, -1, "send");

    //接收结构体cmd判断有无文件 200有 210无

    self_defined_pro_t *server_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    recv(net_fd,server_mes,sizeof(self_defined_pro_t),MSG_WAITALL);

    int file_exit_ser=server_mes->cmd;
    int file_size_ser=server_mes->para_num;//文件总大小
    
    //joint all path name
    char cwd[200] = {0};
    getcwd(cwd, sizeof(cwd));
    //strcat(cwd, "/");
    //strcat(cwd, cs->user);
  //  if(strlen(cs->path) != 1 ){
    //    strcat(cwd, cs->path);
   // }
    strcat(cwd, "/");
    strcat(cwd, file_name);

    if(file_exit_ser==200)
    {   int file_exit_client=open(cwd,O_RDWR|O_APPEND);
        if(file_exit_client < 0)
        {   mes->cmd=-1;//-1为不存在 非0即为文件大小
            send(net_fd,mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
            recv_file(net_fd,cwd);
            //告诉服务端不存在 完全发送
            //准备完全接收
        }else{
            struct stat stat_file;
            fstat(file_exit_client,&stat_file);
            int file_cur_size=stat_file.st_size;
            mes->cmd=file_cur_size;
            //告诉服务端存在，并且发送大小
            send(net_fd,mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

            //准备追加接收

            int cur_size=file_cur_size;
            while(cur_size<file_size_ser)
            {
                char buf[1000]={0};
                int ret;
                if(file_size_ser-cur_size < 1000){
                    ret =recv(net_fd,buf,file_size_ser - cur_size,MSG_WAITALL);
                }else{
                    ret =recv(net_fd,buf,sizeof(buf),MSG_WAITALL);
                }
                if(ret<0)
                {
                    close(file_exit_client);
                    free(server_mes);
                    return -1;
                }
                write(file_exit_client,buf,ret);
                cur_size += ret;
            }

            close(file_exit_client);

        }
        //发送信号告诉服务端是否有文件 及其文件大小
        //存在文件准备接收
    }else{
        printf("文件不存在，下载失败\n");
    }
    return 0;
}

